﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "IApplicationEventHandler.h"
#include "IApplication.h"
#include "IApplicationEvent.h"
#include "IApplicationEventArgs.h"
#include "IDocumentEventHandler.h"
#include "IDocumentManager.h"
#include "IDocumentEvent.h"

#include "GDFoundation/IExtension.h"
#include "ContextualPanel.h"

// 加载资源文件
#include "GbmpFileSystem.h"
#include <QtCore/QResource>


using namespace gdmp;
using namespace gcmp;

namespace Sample
{
    namespace
    {
        QString GetResourceFile() {
            return QString::fromStdWString(gcmp::FileSystem::GetExeDirPath()) + "Gdmp.resources.dll";
        }
    }

    class ExtensionEventHandler final :
        public gcmp::IApplicationEventHandler,
        public gcmp::IDocumentEventHandler
    {
    public:
        ExtensionEventHandler();
        ~ExtensionEventHandler();

        static bool Register() {
            static ExtensionEventHandler object{};
            return true;
        }
    private:
        void On(gcmp::IApplicationEventArgs* pArgs) override;
        void On(gcmp::IDocumentEventArgs* pArgs) override;
    private:
        bool  m_registerResult{ false };
    };

    ExtensionEventHandler::ExtensionEventHandler()
    {
        gcmp::IApplication* app = gcmp::IApplication::Get();
        DBG_WARN_AND_RETURN_VOID_UNLESS(app, L"断言`app`失败",L"GDMPLab",L"2024-03-30");
        if (app) {
            if (auto event = app->GetApplicationEvent()) {
                event->Add(this);
            }
        }
        if (auto obj = gcmp::IDocumentManager::Get()) {
            if (auto event = obj->GetDocumentEvent()) {
                event->Add(this);
            }
        }
        QResource::registerResource(GetResourceFile());
    }

    ExtensionEventHandler::~ExtensionEventHandler()
    {
        if (auto app = gcmp::IApplication::Get()) {
            if (auto event = app->GetApplicationEvent()) {
                event->Delete(this);
            }
        }
        if (auto obj = gcmp::IDocumentManager::Get()) {
            if (auto event = obj->GetDocumentEvent()) {
                event->Delete(this);
            }
        }
        QResource::unregisterResource(GetResourceFile());
    }

    void ExtensionEventHandler::On(gcmp::IApplicationEventArgs* pArgs)
    {
        IExtension::Get().On(pArgs);
    }

    void ExtensionEventHandler::On(gcmp::IDocumentEventArgs* pArgs)
    {
        IExtension::Get().On(pArgs);
    }

    namespace {
        bool stub{ExtensionEventHandler::Register()};
    }    
}
